package gw.com.android.ui.chart.manager;

import android.content.Context;

import com.gwtsz.chart.output.bean.DataParse;
import com.gwtsz.chart.output.customer.MyLineChart;

import com.gwtsz.chart.output.utils.GTTTimeDataModel;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;


import www.com.library.app.Logger;
import www.com.library.util.DoubleConverter;
import www.com.library.util.ProductPrecision;

/**
 * Created by spence on 17-3-14.
 */

public class ChartTimeViewImp implements ChartTimeViewInterface{

    private static String TAG=ChartTimeViewImp.class.getSimpleName();

    private static String METHOD="class="+TAG+" method=";

    private MyLineChart mMyLineChart;

    private ChartInterface ChartDataControl;

    private Context mContext;

    private ProductPrecision mProductPrecision;

    private ArrayList<GTTTimeDataModel> mTimeDataList;

    private boolean isScreenPort=true;

    /** 分时图产品code */
    private int uiCode = 0;
    /** 判断界面是否加载数据成功 */
    public boolean hasSuc = false;

    /** 绘制分时图的起始时间 开市时间 */
    public int mStartTime =0;

    /** 现价 */
    public double mCurPrice = 0;
    /** 昨收价 */
    public double m_endPrice;
    /** 最大的价格值，视图顶部显示的价格值 */
    public double mMaxPrice = 0;
    /** 最小的价格值，视图底部显示的价格值 */
    public double mMinPrice = 0;

    public ChartTimeViewImp(MyLineChart myLineChart,Context context){
        mMyLineChart=myLineChart;
        mContext=context;
        ChartDataControl =TimeChartControl.Build(myLineChart,context);
    }


    @Override
    public void isScreenPort(boolean type) {
        Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName()+",type="+type);
        isScreenPort=type;
    }

    @Override
    public void setProductPrecision(ProductPrecision prdPrecision) {
        Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName()+",prdPrecision="+prdPrecision);
        mProductPrecision=prdPrecision;
    }

    @Override
    public void initPrices(String curPrice, String endPrice, String maxPrice, String minPrice) {
        Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName()+",curPrice="+curPrice+",endPrice="+endPrice+",maxPrice="+maxPrice+",minPrice="+minPrice);
        mCurPrice = DoubleConverter.toDoubleData(curPrice);
        m_endPrice = DoubleConverter.toDoubleData(endPrice);
        mMaxPrice = DoubleConverter.toDoubleData(maxPrice);
        mMinPrice = DoubleConverter.toDoubleData(minPrice);
    }

    @Override
    public boolean hasData() {
        Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName());
        if (null != mTimeDataList && mTimeDataList.size() > 0) {
            return true;
        }

        return false;
    }

    @Override
    public void postInvalidate(){
        Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName());
        //refreshDataToViewControl();
        //mMyLineChart.postInvalidate();
    }

    /**
     * 刷新数据
     */
    private void refreshDataToViewControl(){
        Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName());
        try {

            DataParse dataParse=new DataParse();
            dataParse.parseMinutes(changeData());
            dataParse.setMaxValue(getmMaxPrice());
            dataParse.setMinValue(getmMinPrice());
            dataParse.setBaseValue((float) m_endPrice);
            dataParse.setProductCode(mProductPrecision.getDecimalsPoint());
            ChartDataControl.refreshData(dataParse);
        }catch (JSONException e){

        }
    }

    private double getmMaxPrice(){
        Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName());
        if(mMaxPrice==mMinPrice) {
            return mMaxPrice*1.05;
        }
        return mMaxPrice - m_endPrice > m_endPrice - mMinPrice ? mMaxPrice
            : 2 * m_endPrice - mMinPrice;
    }

    private double getmMinPrice(){
        Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName());
        if(mMaxPrice==mMinPrice) {
            return mMinPrice*0.95;
        }
        return m_endPrice-mMinPrice>mMaxPrice-m_endPrice?mMinPrice:2*m_endPrice-mMaxPrice;
    }

    @Override
    public void setStartTime(int startTime) {
        Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName()+",startTime="+startTime);
        mStartTime = startTime;
        ChartDataControl.setStartTime(mStartTime);
        /*if(null != m_renderer){
            m_renderer.setStartDate(startTime);
        }*/
        setXMarkerList();
    }
    /**
     * 时间轴坐标点集合
     */
    private void setXMarkerList(){
        /*if(m_plotWidth == 0){
            return;
        }
        int preWidth = m_plotWidth/numOfGridX;
        xMarkerList.clear();
        for (int i = 0; i < numOfGridX + 1; i++) {
            AxisMarker marker = new AxisMarker();
            int nextTime = mStartTime + 24 / numOfGridX * i * 60 * 60;
            marker.mIntMarker = nextTime;
            marker.X = i * preWidth;
            marker.Y = m_plotHeight - BOTTOMPADDING/2;
            xMarkerList.add(marker);
        }
        mStartTime = xMarkerList.get(0).mIntMarker;*/
    }

    @Override
    public void refreshData(int uiCode) {
        Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName()+",uiCode="+uiCode);
        this.uiCode = uiCode;
        GTTTimeDataModel lastTimeRound=null;
        if(mTimeDataList.size() > 0){
            lastTimeRound = mTimeDataList.get(mTimeDataList.size() - 1);
        }

        if(lastTimeRound != null){
            //GTTKDataManager.instance().lastTime = lastTimeRound.mTime;
            hasSuc = true;
        }
        //postInvalidate();
    }

    @Override
    public void invalidate() {
        Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName());
        refreshDataToViewControl();
        mMyLineChart.invalidate();
    }

    private void cleanTimeDataList(){
        mTimeDataList=null;
    }

    public void setTimeDataList(ArrayList<GTTTimeDataModel> list){
        Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName()+",list="+list.toString());
        mTimeDataList=new ArrayList<>();
        mTimeDataList.addAll(list);
    }

    /**
     * 将数据转换为图表数据
     */
    public JSONObject changeData() throws JSONException{
        Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName());
        JSONArray jsonArray=new JSONArray();

        for (GTTTimeDataModel gttTimeDataModel:mTimeDataList){
            jsonArray.put(changeTimeToHHMM(gttTimeDataModel.mTime)+" "+gttTimeDataModel.mPrice+" "+gttTimeDataModel.mAvgPrice);
        }
        String jsondataString = "{'date':'20160613'}";
        JSONObject jsondataArray=new JSONObject(jsondataString);
        jsondataArray.put("data",jsonArray);
        JSONObject jsondata=new JSONObject();
        jsondata.put("data",jsondataArray);
        JSONObject jsonsz0022081=new JSONObject();
        jsonsz0022081.put("sz002081",jsondata);
        String jsonCodeString = "{'code':0}";
        JSONObject jsoncode=new JSONObject(jsonCodeString);
        jsoncode.put("data",jsonsz0022081);
        return jsoncode;
    }

    private String changeTimeToHHMM(long time){
        Calendar calendar=Calendar.getInstance();
        calendar.setTimeInMillis(time*1000);
        return formatTime(calendar.get(Calendar.HOUR_OF_DAY))+""+formatTime(calendar.get(Calendar.MINUTE));
    }

    private String formatTime(int number){
        String format=number+"";
        if(number<10){
            format="0"+number;
        }
        return format;
    }

    @Override public void refreshDecimalNum(int num) {
        ChartDataControl.refreshProductDecimalNum(num);
    }
}
